package bs.app.Client;

import bs.app.Component.TextField;
import bs.app.Constant.CConfig;
import bs.app.Constant.KeyConstant;
import bs.app.Main.CCanvas;
import bs.app.Position.CPosition;
import bs.app.Res.CRess;
import bs.app.android.Graphics;


public class CardPage 
{
	public TextField textfield;
	public int xTextField,yTextField,wTextField,hTextField;
    public int timeForLoading = 50;
    public String Link;
    public String Title="Nạp thẻ cào";
    public final String  s_keyboard[] ={"0","1","2","3","4","5","6","7","8","9",
    									"q","w","e","r","t","y","u","i","o","p",
    									"a","s","d","f","g","h","j","k","l"," ",
    									"z","x","c","v","b","n","m",",",".","?"};
    public final int iRow = 3;
    public final int iCol = 10;
    public final int iH = 32;
    public final int iW = 22;
	public CardPage()
	{
		
	}
	public void init(String []link)
	{					
		wTextField=183;
		hTextField=16;
		xTextField=CCanvas.s_iWcanvas/2-wTextField/2-15;
		yTextField=CCanvas.s_iHcanvas - CRess.keyboard.getHeight()-CConfig.HIEGHT_COMMNAND - hTextField - 8;
		textfield=new TextField(true);
		textfield.setBound(xTextField, yTextField, wTextField, hTextField);
		this.Title = link[0];
		this.Link=link[1];
	}
   public void init(String link, String tilte)
	{
		wTextField=183;
		hTextField=16;
		xTextField=CCanvas.s_iWcanvas/2-wTextField/2-15;
		yTextField=CCanvas.s_iHcanvas - CRess.keyboard.getHeight()-CConfig.HIEGHT_COMMNAND - hTextField -8;
		textfield=new TextField(true);
		textfield.setBound(xTextField, yTextField, wTextField, hTextField);
		this.Link=link;
        this.Title=tilte;
	}
	public void Painttransparent(Graphics g)
	{
		g.drawImage(isChangeLoadingState.imgtransparent,CCanvas.xTranslate, CCanvas.yTranslate+25, Graphics.TOP|Graphics.LEFT);
	}
	public void painttextfield(Graphics g)
	{
			g.drawImage(CRess.menubar,0, CCanvas.s_iHcanvas, CPosition.ANCHOR_LEFT_TOP);		
			g.drawImage(CRess.keyboard, (CCanvas.s_iWcanvas-CRess.keyboard.getWidth())/2, CCanvas.s_iHcanvas - CRess.keyboard.getHeight()-CConfig.HIEGHT_COMMNAND, CPosition.ANCHOR_LEFT_TOP);
            textfield.paint(g,Title);
			CCanvas.mediumfont.drawString(g, "Gửi",CCanvas.s_iWcanvas/2-CCanvas.mediumfont.stringWidth("Gửi")/2,CCanvas.s_iHcanvas -CConfig.FONT_HEIGHT_COMMAND, 0, 0xa1a6a1, 0x090b7a, true);
			if(textfield.TextString.trim().equals(""))
			{
				CCanvas.mediumfont.drawString(g, "Quay Lại", CCanvas.s_iWcanvas -CCanvas.mediumfont.stringWidth("Quay Lại")-3, CCanvas.s_iHcanvas - CConfig.FONT_HEIGHT_COMMAND, 0, 0xa1a6a1, 0x090b7a, true);
			}
			else
			{
				CCanvas.mediumfont.drawString(g, "Xóa", CCanvas.s_iWcanvas - CCanvas.mediumfont.stringWidth("Xóa")-1, CCanvas.s_iHcanvas -CConfig.FONT_HEIGHT_COMMAND, 0, 0xa1a6a1, 0x090b7a, true);
			}
	}
	
	public int pointerItems(int x,int y){	
		int x0 = (CCanvas.s_iWcanvas - CRess.keyboard.getWidth())/2+4;
		int x1 =(CCanvas.s_iWcanvas - CRess.keyboard.getWidth())/2+ CCanvas.s_iWcanvas-4;
		int y0 = CCanvas.s_iHcanvas -CRess.keyboard.getHeight()+4 - CConfig.HIEGHT_COMMNAND;
		int y1 = CCanvas.s_iHcanvas + CRess.keyboard.getHeight()-4 -CConfig.HIEGHT_COMMNAND;				
		if(x>= x0 && x<= x1 && y>=y0 && y<= y1){
			y = y - y0;
			x = x - x0;			
			int iIndex = ((y+iH)/iH -1)*iCol + ((x+iW)/iW -1);
			if(iIndex>=0 && iIndex< s_keyboard.length && textfield.TextString.length() <TextField.maxTextLength){
				textfield.TextString +=s_keyboard[iIndex];
				textfield.caretPos++;
				textfield.widthCaret=0;
			}
		}
		return -1;
	}
	public void pointerPressed(int px,int py)
	{
		if(CCanvas.isPointerPressed) //  User PointerPressed
		{
			if (Pointer.SelectSoftKey(px, py)==3){				
				CCanvas.keyPressed[KeyConstant.KEY_RIGHT_SOFTKEY]=true;
			}else if(Pointer.SelectSoftKey(px, py)==1 || Pointer.SelectSoftKey(px, py)==2){
				CCanvas.keyPressed[KeyConstant.KEY_FIRE]=true;
			}else {	//click into items 
				pointerItems( px,py);
			}
		}
		CCanvas.resetPointerPress();  // User PointerReleased
	}
	public void UpdateKeyPress(boolean[]keyPressed,boolean[]keyHold,int currentAsccii[])
	{
		update();
		if(CCanvas.s_iCurrentAsciiKeyPressed[0]>=65&&CCanvas.s_iCurrentAsciiKeyPressed[0]<=122)
		{
			textfield.keyPressedAscii(CCanvas.s_iCurrentAsciiKeyPressed[0]);
			CCanvas.s_iCurrentAsciiKeyPressed[0]=0;
			CCanvas.resetkey();
			CCanvas.resetPointerPress();
			return;
    	}
		// dung cho ban phim thuong
		if(CCanvas.currentkeycode>=48 && CCanvas.currentkeycode<=57)
		{
			for(int i=48;i<=57;i++)
			{
				if(CCanvas.keyPressed[i-48])
				{
					textfield.KeyPressAny(i);
				}
			}
		}
		else if(keyPressed[KeyConstant.KEY_FIRE])   // User PressKey Fire
		{
			if(!textfield.TextString.equals(""))
			{	
				//System.out.println("CCanvas.SaveStatePrevious:"+ CCanvas.SaveStatePrevious);
				CCanvas.CurrentScreen = CCanvas.SaveStatePrevious;
				CCanvas.s_Instan.connect.setURL(this.Link + "&value="+ textfield.TextString.trim() );				
				CCanvas.s_Instan.connect.RequestServerGetData();
				
			}
		}
		else if(keyPressed[KeyConstant.KEY_LEFT_SOFTKEY])   
		{
			
		}
		else if(keyPressed[KeyConstant.KEY_RIGHT_SOFTKEY])   
		{
			if (textfield.caretPos > 0 && textfield.TextString.length() > 0)
	        {
	            textfield.TextString = textfield.TextString.substring(0, textfield.caretPos - 1) + textfield.TextString.substring(textfield.caretPos, textfield.TextString.length());
	            textfield.caretPos--;
	        }
			else
			{
				//System.out.println("1111CCanvas.SaveStatePrevious:"+ CCanvas.SaveStatePrevious);
				CCanvas.CurrentScreen = CCanvas.SaveStatePrevious;
				if(CCanvas.s_Instan.connect.Link.size()>0)
					CCanvas.s_Instan.connect.Link.removeElementAt(CCanvas.s_Instan.connect.Link.size()-1);
			}
		}
		CCanvas.s_iCurrentAsciiKeyPressed[0]=0;
		textfield.update();
		CCanvas.resetkey();
	}
	public void update()
	{
		
	}
}
